//
//  ZJDataCache.m
//  DataCache
//
//  Created by zhengju on 16/6/30.
//  Copyright © 2016年 zhengju. All rights reserved.
//  下载请保留出处  https://github.com/zhengju/DataCache

#import "ZJDataCache.h"

#import "NSString+Hashing.h"

@interface ZJDataCache ()

@property (nonatomic,assign) NSTimeInterval invaliteTime;//有效时间

@end

@implementation ZJDataCache
//创建单例对象
static ZJDataCache *cache = nil;
/**
 *  @author zhengju, 16-06-30 17:06:27
 *
 *  @brief 单例创建缓存对象
 *
 *  @return 单例对象
 */
+(ZJDataCache *)sharedCache{
    @synchronized(self){
        if (!cache) {
            cache = [[[ZJDataCache class] alloc]init];
        }
    }
    return cache;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone{
    @synchronized(self){
        if (!cache) {
            cache = [super allocWithZone:zone];
        }
    }
    return cache;
}
/**
 *  @author zhengju, 16-06-30 17:06:49
 *
 *  @brief 初始化的时候返设置过期时间
 *
 *  @return 对象
 */
-(id)init{
    
    if (self = [super init]) {
 
        _invaliteTime = 60*60;//以秒为单位
    }
    return self;
}

/**
 *  @author zhengju, 16-06-30 17:06:43
 *
 *  @brief 存数据
 *
 *  @param data 缓存Data数据
 *  @param name 段路径，一般用请求数据的短URL来传值
 *
 *  @return 是否保存数据成功
 */
-(BOOL)saveDataWithData:(NSData *)data andStringName:(NSString *)name{
    //获取路径
    NSString *path = [NSString stringWithFormat:@"%@/Documents/Cache/",NSHomeDirectory()];//沙盒路径
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
   BOOL isSuc = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    
    if (!isSuc) {
        NSLog(@"创建失败");
        return NO;
    }
    
    //先将文件名字进行加密处理
    //MD5:一种加密方式,通过MD5加密会得到一个16进制的32位的文件(固定长度)
    
    name = [name MD5Hash];
    
    //获取的完整路径
    NSString *allPath = [NSString stringWithFormat:@"%@%@",path,name];
    
    BOOL isWriteSuc = [data writeToFile:allPath atomically:YES];//写文件
    
    return isWriteSuc;
}

/**
 *  @author zhengju, 16-06-30 17:06:44
 *
 *  @brief 根据路径查找Data数据
 *
 *  @param name 段路径
 *
 *  @return 返回缓存的在段路径下的Data数据
 */
-(NSData *)getDataWithStringName:(NSString *)name{
    
    NSString *tempName = [name MD5Hash];
    
    NSString *path = [NSString stringWithFormat:@"%@/Documents/Cache/%@",NSHomeDirectory(),tempName];
   // NSLog(@"--path----->>%@",path);
    //判断文件是否存在
  
    NSFileManager *manage  = [NSFileManager defaultManager];
   
    if (![manage fileExistsAtPath:path]) {
        NSLog(@"文件不存在");
        return nil;
    }
    
    //判断数据是否过期
 
    NSTimeInterval invalitTime = [[NSDate date] timeIntervalSinceDate:[self getLastModefityDateWithFile:path]];
    
    if (invalitTime >= _invaliteTime) {
        return nil;
    }
    //取数据
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    return data;
}

//获取最后修改文件的日期
-(NSDate *)getLastModefityDateWithFile:(NSString *)path{
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSDictionary  *dic = [manager attributesOfItemAtPath:path error:nil];
    /*
     NSFileCreationDate = "2015-08-10 03:38:15 +0000";
     NSFileExtensionHidden = 0;
     NSFileGroupOwnerAccountID = 20;
     NSFileGroupOwnerAccountName = staff;
     NSFileModificationDate = "2015-08-10 03:38:15 +0000";
     NSFileOwnerAccountID = 501;
     NSFilePosixPermissions = 420;
     NSFileReferenceCount = 1;
     NSFileSize = 17090;
     NSFileSystemFileNumber = 9204533;
     NSFileSystemNumber = 16777217;
     NSFileType = NSFileTypeRegular;
     */
  //  NSLog(@"%@",dic);
    
    return dic[NSFileModificationDate];
    
}
@end






